package dda;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Random;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class SortClient {
	Random r = new Random();
	Comparable sortedList[] = new Comparable[20];
	private int counter;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SortClient sc = new SortClient();
		try {
			sc.sort();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void sort() throws MalformedURLException, RemoteException,
			NotBoundException {
		// ISortFactory isf;
		// isf = (ISortFactory) Naming.lookup("//localhost:1099/sortfactory");

		Comparable list[] = new Comparable[] { 58, 23, 25, 19, 8, 76, 86, 83,
				54, 24, 36, 81, 78, 12, 4, 2, 27, 45, 1, 95 };
		Comparable[][] lists = splice(list);
		ListJoiner lj = new ListJoiner();
		for (int i = 0; i < 3; i++) {
			SorterThread st = new SorterThread(lists[i], i);
			st.addObserver(lj);
			st.start();
		}
	}

	private Comparable[] generateRandomIntegers(int length) {
		Comparable list[] = new Comparable[length];
		for (int i = 0; i < length; i++) {
			list[i] = r.nextInt(length * 2);
		}
		return list;

	}

	private Comparable[] generateRandomString(int length) {
		Comparable[] list = new Comparable[length];
		Random random = new Random();

		for (int i = 0; i < length; i++) {
			int wordSize = random.nextInt(10);
			String generatedWord;

			char[] word = new char[wordSize];

			for (int i1 = 0; i1 < wordSize; i1++) {
				int randomNo = random.nextInt(255);
				word[i1] = (char) randomNo;
				while ((randomNo < 65 ^ randomNo > 90)) {
					randomNo = random.nextInt(255);
					word[i1] = (char) randomNo;
				}
			}
			generatedWord = new String(word);
		}

		return list;

	}

	private Comparable[][] splice(Comparable list[]) {

		int splitter = 0;
		for (int i = 0; i < list.length; i++) {
			if ((Integer) list[i] > splitter) {
				splitter = (Integer) list[i];
			}
		}

		ArrayList<Comparable> deel1 = new ArrayList<Comparable>(splitter);
		ArrayList<Comparable> deel2 = new ArrayList<Comparable>(splitter);
		ArrayList<Comparable> deel3 = new ArrayList<Comparable>(splitter);
		Comparable opgedeeldeLijst[][] = new Comparable[3][splitter];

		splitter = splitter / 3;

		for (int i = 0; i < list.length; i++) {
			if ((Integer) list[i] < splitter) {
				deel1.add(list[i]);
			} else if ((Integer) list[i] > splitter
					&& (Integer) list[i] < (splitter * 2)) {
				deel2.add(list[i]);
			} else {
				deel3.add(list[i]);
			}
		}

		opgedeeldeLijst[0] = deel1.toArray(new Comparable[deel1.size()]);
		opgedeeldeLijst[1] = deel2.toArray(new Comparable[deel2.size()]);
		opgedeeldeLijst[2] = deel3.toArray(new Comparable[deel3.size()]);

		return opgedeeldeLijst;
	}

	private void join(Comparable[] list) {
		System.arraycopy(list, 0, sortedList, counter, list.length);
		counter = counter + list.length;

	}
}